package com.platform.usercenter.utils;

import com.platform.common.core.utils.StringUtils;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

public class ListUtil {

    /**
     * 分割字符串
     * @param str
     * @param split
     * @return
     */
    public static List<String> split(String str, String split){
        List<String> result = new ArrayList<String>();
        if(EmptyUtil.isHaveEmpty(str, split)){
            result.add(str);
            return result;
        }
        String[] strs = str.split(split);
        for(String one : strs){
            if(!EmptyUtil.isHaveEmpty(one)){
                result.add(one);
            }
        }
        return result;
    }

    /**
     * 将字符串集合转换成指定类型集合
     * @param list
     * @param claz
     * @return
     * @throws Exception
     */
    public static<T> List<T> convey(List<String> list, Class<T> claz) throws Exception{
        List<T> result = new ArrayList<T>();
        if(EmptyUtil.isHaveEmpty(claz) || EmptyUtil.isEmpty(list)){
            return result;
        }else if(claz.equals(String.class)){
            return (List<T>) list;
        }
        Constructor<T> constructor = claz.getConstructor(String.class);
        for(String one : list){
            result.add((T) constructor.newInstance(one));
        }
        return result;
    }
}
